#include <stdarg.h>
#include <unistd.h>
#include <stdio.h>
#include <time.h>
#include <string.h>
#include <assert.h>
#include <errno.h>
#include <pthread.h>
#include <syslog.h>
#include <sys/syscall.h>
//--------------------------
#include "my_types.h"
#include "dbg_trace.h"


/**************************************************************************************
										变量区
***************************************************************************************/
unsigned int G_u32OutputStart = 0;
char G_szOutput[MAX_OUTPUT_CNT][512];
static pthread_mutex_t G_tTraceLock = PTHREAD_MUTEX_INITIALIZER;
static unsigned char G_u8TraceIndex = 0;
MY_BOOL bDbgFlag = MY_TRUE;
/**************************************************************************************/


/************************************************************
函数名称：ma_syslog_start
函数功能：启动syslogd功能
参	  数：
返 回 值：
说	  明：
************************************************************/
MY_VOID dbg_syslog_start(MY_VOID)
{  
	MY_CHAR cmd[100];
	
	memset(cmd, '\0', sizeof(cmd));
	sprintf(cmd, "rm -f %s/*;syslogd -O %s -s 60 -b 1;", SYSLOG_DIR, SYSLOG_FILE_WORK);
	system(cmd);
}


//调试打印函数
void DbgTrace( const char* _szColor, const char* _szPrefix, const char* _szFile, 
				   unsigned int _u32Line, const char* _szFunction, const char* _szFmt, ...)
{	
	if(bDbgFlag == MY_FALSE)
		return;

    static char tempflag = 0;
    if(0 == tempflag)
    {
//		dbg_syslog_start();
        tempflag = 1;
    }

	pthread_mutex_lock(&G_tTraceLock);
	int s32ErrnoCpy = errno;
	char* szBasename = strrchr(_szFile, '/');
	if(szBasename == NULL)
	{
		szBasename = (char*)_szFile;
	}
	time_t timet = time(NULL);
	struct tm timetm;
	localtime_r(&timet, &timetm);
	long int threadid = syscall(224);
	if(threadid == -1)
	{
		threadid = (long int)getpid();
	}

	unsigned int u32Using;
	unsigned int u32Left;

	MY_ASSERT(G_u32OutputStart < MAX_OUTPUT_CNT, "G_u32OutputStart %u is invalid", G_u32OutputStart);
	memset(G_szOutput[G_u32OutputStart], 0, sizeof(G_szOutput[G_u32OutputStart]));

	u32Using = strlen(G_szOutput[G_u32OutputStart]);
	u32Left = sizeof(G_szOutput[G_u32OutputStart]) - u32Using;
	snprintf(G_szOutput[G_u32OutputStart] + u32Using, u32Left, "%s%04d-%02d-%02d %02d:%02d:%02d [%02hhu] %04ld %6s %s:%s:%u "NONE,
			_szColor, 
			timetm.tm_year+1900, timetm.tm_mon+1, timetm.tm_mday, timetm.tm_hour, timetm.tm_min, timetm.tm_sec, 
			G_u8TraceIndex, threadid, _szPrefix, szBasename, _szFunction, _u32Line);

	u32Using = strlen(G_szOutput[G_u32OutputStart]);
	u32Left = sizeof(G_szOutput[G_u32OutputStart]) - u32Using;
	va_list argptr;
	va_start(argptr, _szFmt);
	vsnprintf(G_szOutput[G_u32OutputStart] + u32Using, u32Left, _szFmt,
			argptr);
	va_end(argptr);

	u32Using = strlen(G_szOutput[G_u32OutputStart]);
	u32Left = sizeof(G_szOutput[G_u32OutputStart]) - u32Using;

	G_szOutput[G_u32OutputStart][u32Using] = '\n';
	G_szOutput[G_u32OutputStart][sizeof(G_szOutput[0]) - 2] = '\n';
	G_szOutput[G_u32OutputStart][sizeof(G_szOutput[0]) - 1] = 0;
	
#ifndef ANDROID_LOG
	printf("%s", G_szOutput[G_u32OutputStart]);
#else
	LOGI("%s", G_szOutput[G_u32OutputStart]);
#endif
	
#ifndef ROM_TOOL_MAIN
	char* pos = strstr(G_szOutput[G_u32OutputStart], NONE);
	strcpy(pos, pos + strlen(NONE));
	char *pszTmp = strchr(G_szOutput[G_u32OutputStart], 'm');
#endif	

	G_u32OutputStart ++;
	G_u32OutputStart %= MAX_OUTPUT_CNT;
	G_u8TraceIndex = (G_u8TraceIndex < 99) ? (G_u8TraceIndex + 1) : 0;

	errno = s32ErrnoCpy;
	pthread_mutex_unlock(&G_tTraceLock);
}





